package com.open.pokergames.biz.views.invite.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.open.pokergames.NetConfig;
import com.open.pokergames.R;
import com.open.pokergames.biz.entity.MemberEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;
import com.sinothk.view.image.rounded.RoundedImageView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MemberListAdapter extends RecyclerView.Adapter<MemberListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<MemberEntity> mData;
    private Context mContext;
    private OnSuperListener<MemberEntity> superListener;

    public MemberListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<MemberEntity> superListener) {
        this.superListener = superListener;
    }

    public void setData(List<MemberEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<MemberEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public List<MemberEntity> getData() {
        return this.mData;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.invite_search_member_adapter_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final MemberEntity itemData = mData.get(position);
        holder.nameTv.setText(XUtils.string().getNotNullValue(itemData.getUserName()));
        holder.msgTv.setText(XUtils.string().getNotNullValue(itemData.getAccount()));

        holder.inviteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                superListener.onClick(position, itemData, "inviteTv");
            }
        });
        Glide.with(mContext).load(NetConfig.getImageUrl(itemData.getUserAvatar()))
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .into(holder.imageView);
//        if (superListener != null) {
//            holder.rootView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    superListener.onClick(position, itemData, "");
//                }
//            });
//        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rootView;
        TextView nameTv, msgTv, stateTv, inviteTv;
        RoundedImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            imageView = itemView.findViewById(R.id.imageView);

            nameTv = itemView.findViewById(R.id.nameTv);
            msgTv = itemView.findViewById(R.id.msgTv);

            stateTv = itemView.findViewById(R.id.stateTv);
            inviteTv = itemView.findViewById(R.id.inviteTv);
        }
    }
}